<meta charset="utf-8">
<title>Blogger Random Post Widget</title>
<style>
body{background:#f2f2f2; font-size:12px; font-family:Arial, Helvetica, sans-serif}
a{color:#1A74BA;text-decoration:none;outline:none;}
a:hover, a:active{color:#174271;text-decoration:underline;}
#random-post-load{background: url(http://1.bp.blogspot.com/-2oJZT1D_IdU/VBj0TRf2EUI/AAAAAAAACH0/PVf_s0ifMrw/s1600/ajax-loader.gif) no-repeat; padding-left:20px;}
#random-post-container {width:500px}
#random-post-container ul{padding:0;}
#random-post-container li {
  padding:10px 0 10px 0;
  list-style:none;
  overflow:hidden;
  border-top:1px solid #CCC;
}
#random-post-container img {
  display:block;
  float:left;
  margin:2px 7px 5px 0;
}
#random-post-container a {
  font-weight:normal;
  font-size:100%;
}
#rancom-post-container .clear {
  display:block;
  clear:both;
}
.categories{background:url(http://2.bp.blogspot.com/-H_9Y0VCbjY8/VCAqwUaopUI/AAAAAAAACIU/UwHMsTW7yh4/s1600/icon-article-tags.png) 0 center no-repeat;padding:0 0 0 20px;float:left;}
</style>

<h2>Bài viết ngẫu nhiên</h2>
<div id='random-post-container'><div id="random-post-load">Đang tải dữ liệu...</div></div>

<script type="text/javascript">
var homePage = 'http://www.nhatchanh.info', //Địa chỉ Blog của bạn
    maxResults = 5, //Số bài viết hiển thị
    author_date_commentShow = true; //Hiển thị tên Tác giả + Ngày tháng + Nhận xét
    authorShow = true, //Hiển thị tên tác giả
    commentName = 'Nhận xét',
    thumbnail = true, //Sử dụng hình thumbnail
    imgSize = 72, //Kích thước hình thumbnail
    noImageUrl ='http://2.bp.blogspot.com/-cU2FNhVMvtY/VBFZ_d5FLYI/AAAAAAAACEo/GPcK7-uQ5DE/s1600/default.gif',
    summaryShow = true, //Hiển thị nội dung tóm tắt
    summaryLength = 180, //Số ký tự hiển thị cho nội dung tóm tắt
    labelShow = true, //Hiển thị nhãn label
    maxResultsLabel = 10, //Số bài viết trả về của nhãn
    containerId = 'random-post-container';
</script>
<script type="text/javascript">
function getRandomInt(min, max) {
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

function shuffleArray(arr) {
	var i = arr.length, j, temp;
	if (i === 0) return false;
	while (--i) {
		j = Math.floor(Math.random() * (i + 1));
		temp = arr[i];
		arr[i] = arr[j]; 
		arr[j] = temp;
	}
	return arr;
}

function createRandomPostsStartIndex(json) {
	var startIndex = getRandomInt(1, (json.feed.openSearch$totalResults.$t - maxResults));
	if (window.console && window.console.log) console.log('Get the post feed start from '+startIndex+' until ' +(startIndex + maxResults)); 
	document.write('<scr'+'ipt src="'+homePage+'/feeds/posts/summary?alt=json-in-script&orderby=updated&start-index='+startIndex+'&max-results='+maxResults+ '&callback=randomPosts"></scr'+'ipt>');
}

function randomPosts(json) {
	var link, summary, img,
		ct = document.getElementById(containerId),
		entry = shuffleArray(json.feed.entry),
		html = "<ul>";
	for (var i = 0, len = entry.length; i < len; i++) {
		summary = ("summary" in entry[i]) ? (entry[i].summary.$t.replace(/<.*?>/g, "")).substring(0, summaryLength) + '&hellip;' : "";
		img = ("media$thumbnail" in entry[i]) ? entry[i].media$thumbnail.url.replace(/\/s[0-9]+(-c)?/, "/s72-c") : noImageUrl;
		date=entry[i].published.$t;

		author = entry[i].author[0].name.$t;
		author_url = '<a href="'+entry[i].author[0].uri.$t+'" target="_blank">'+author+'</a>';

		avatar = entry[i].author[0].gd$image.src.replace(/\/s[0-9]+(\-c)?/,"/s36-c").replace(/http\:/,"");

		for (var j = 0, jen = entry[i].link.length; j < jen; j++) {
			link = (entry[i].link[j].rel == "alternate") ? entry[i].link[j].href : '#';
		}

		commentsNum='<a href="'+link+'#comment-form">'+entry[i].thr$total.$t+' '+commentName+'</a>';

		html += '<li>';
		if(thumbnail==true){
		html += '<img src="'+img+'" alt="" width="'+imgSize+'" height="'+imgSize+'">';
		}
		html += '<a href="'+link+'">'+entry[i].title.$t+'</a><br>';
		
		
		if(author_date_commentShow==true){
			if(authorShow==true){
				html +=author_url+' | ';
			}
			html +=date.substring(8,10)+'/'+date.substring(5,7)+'/'+date.substring(0,4)+' | '+commentsNum+'<br />';
		}
		
		if(summaryShow==true){
		html += '<span>'+summary+'</span>';
		}
		
		if(labelShow==true){
		html += " <br /><div class='categories'>";
			var tags = entry[i].category,
				labels = [];
			for(var z = 0, zen = tags.length; z < zen; ++z) {
				labels.push('<a href="' + homePage + '/search/label/' + encodeURIComponent(tags[z].term) + '?max-results='+maxResultsLabel+'" rel="tag">' + tags[z].term + '</a>');
			}
			html += labels.join(', ');
		html += " </div>";
		}
		
		html += '<span class="clear"></span></li>';
	}
	ct.innerHTML = html + '</ul>';
}
document.write('<scr'+'ipt src="'+homePage+'/feeds/posts/summary?alt=json-in-script&max-results=0&callback=createRandomPostsStartIndex"></scr'+'ipt>');
</script>
